package utils

import (
	cryptoRand "crypto/rand"
	"encoding/base64"
	mathRand "math/rand"
	"time"
)

// 初始化随机数生成器
func init() {
	mathRand.Seed(time.Now().UnixNano())
}

// GenerateRandomString 生成指定长度的随机字符串
func GenerateRandomString(length int) string {
	b := make([]byte, length)
	cryptoRand.Read(b)
	return base64.URLEncoding.EncodeToString(b)[:length]
}

// RandomString 生成指定长度的随机字符串
func RandomString(length int) string {
	const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	result := make([]byte, length)
	for i := range result {
		result[i] = charset[mathRand.Intn(len(charset))]
	}
	return string(result)
}
